package org.jing.ext.extra;

import org.jing.core.lang.Const;
import org.jing.core.util.GenericUtil;
import org.jing.core.util.StringUtil;

import java.math.BigDecimal;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Description: <br>
 *
 * @author bksmate <br>
 * @since 2025-03-26 <br>
 */
public class TextHelper {
    private static final Map<Character, Integer> CHINESE_TO_ARABIC_MAP = GenericUtil.<Character, Integer>buildMap()
        .put('零', 0)
        .put('一', 1)
        .put('二', 2)
        .put('两', 2)
        .put('三', 3)
        .put('四', 4)
        .put('五', 5)
        .put('六', 6)
        .put('七', 7)
        .put('八', 8)
        .put('九', 9)
        .put('十', 10)
        .put('百', 100)
        .put('千', 1000)
        .put('万', 10000)
        .put('亿', 100000000)
        .get();

    private TextHelper() {}

    @Deprecated
    public static int convertChineseToArabicDeprecated(String chineseNumber) {
        if (chineseNumber.length() == 1 && CHINESE_TO_ARABIC_MAP.containsKey(chineseNumber.charAt(0))) {
            return CHINESE_TO_ARABIC_MAP.get(chineseNumber.charAt(0));
        }
        int result = 0;
        int temp = 0;
        int pos = 0;
        for (char c : chineseNumber.toCharArray()) {
            if (CHINESE_TO_ARABIC_MAP.containsKey(c)) {
                int current = CHINESE_TO_ARABIC_MAP.get(c);
                if (current >= 10) {
                    if (temp != 0) {
                        result += temp * current;
                    }
                    else {
                        result += current;
                    }
                    temp = 0;
                } else {
                    temp += current;
                }
            }
            pos ++;
        }
        // 处理最后一位数字
        result += temp;
        return result;
    }

    // 将中文数字转换为阿拉伯数字
    public static BigDecimal convertChineseToArabic(String chineseNumber) {
        if (StringUtil.isEmpty(chineseNumber)) return Const.NUM_0;
        Pattern specialRegex = Pattern.compile("^(.*?)([一二三四五六七八九]?)([百千万亿])([一二三四五六七八九])$");
        Matcher matcher = specialRegex.matcher(chineseNumber);
        if (matcher.find()) {
            String prefix = matcher.group(1);
            String rate = StringUtil.ifEmpty(matcher.group(2), "一");
            String place = matcher.group(3);
            String sub = matcher.group(4);
            int placeNum = CHINESE_TO_ARABIC_MAP.get(place.charAt(0));
            BigDecimal temp = BigDecimal.valueOf(CHINESE_TO_ARABIC_MAP.get(rate.charAt(0))).multiply(BigDecimal.valueOf(placeNum))
                .add(new BigDecimal(CHINESE_TO_ARABIC_MAP.get(sub.charAt(0))).multiply(BigDecimal.valueOf(placeNum / 10)));
            return convertChineseToArabic(prefix).add(temp);
        }
        BigDecimal result = Const.NUM_0;
        BigDecimal temp = Const.NUM_0;
        boolean flag = false;
        for (char c : chineseNumber.toCharArray()) {
            if (CHINESE_TO_ARABIC_MAP.containsKey(c)) {
                int current = CHINESE_TO_ARABIC_MAP.get(c);
                // 特殊倍数
                if (current >= 10000) {
                    result = result.add(temp);
                    result = result.max(Const.NUM_1).multiply(BigDecimal.valueOf(current));
                    temp = Const.NUM_0;
                    flag = false;
                }
                // 倍数
                else if (current >= 10) {
                    temp = temp.max(Const.NUM_1).multiply(BigDecimal.valueOf(current));
                    flag = true;
                }
                else {
                    if (flag) {
                        result = result.add(temp);
                    }
                    temp = BigDecimal.valueOf(current);
                    flag = !flag;
                }
            }
        }
        // 处理最后一位数字
        result = result.add(temp);
        return result;
    }

    public static void main(String[] args) {
        System.out.println(Const.NUM_100.max(Const.NUM_10));
        temp("三万亿亿零七千八百四十二");
        temp("万亿亿零七千八百四十二");
        temp("三千四百万");
        temp("一亿二");
        temp("四万三百二");
        temp("四万三");
        temp("两千零二十");
        temp("两千零二");
        temp("千八");
        temp("二百五十三");
        temp("二百五十");
        temp("二百五");
        temp("二百零三");
        temp("九十九");
        temp("十二");
        temp("三");
    }

    private static void temp(String chineseNumber) {
        System.out.println(chineseNumber + "|" + convertChineseToArabic(chineseNumber));
    }
}
